using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace ClientTools.Server.Controls
{
    public partial class LogViewer : System.Web.UI.Page
    {
        private Library.Logging.LogManager _LogManager;

        protected void Page_Load(object sender, EventArgs e)
        {
            _LogManager = new ClientTools.Library.Logging.LogManager(umbraco.GlobalSettings.DbDSN);
            if (!this.IsPostBack)
            {
                this.FillHeaders();
                this.GetLogs();
            }
        }

        protected void cmdCheckAll_Click(object sender, ImageClickEventArgs e)
        {
            foreach (GridViewRow row in GridView1.Rows)
            {
                CheckBox checkbox = (CheckBox)row.FindControl("chkRows");
                checkbox.Checked = true;
            }
        }

        protected void cmdUncheckAll_Click(object sender, ImageClickEventArgs e)
        {
            foreach (GridViewRow row in GridView1.Rows)
            {
                CheckBox checkbox = (CheckBox)row.FindControl("chkRows");
                checkbox.Checked = false;
            }
        }

        protected void cmdGetLogs_Click(object sender, ImageClickEventArgs e)
        {
            this.GetLogs();
        }

        protected void cmdResetFilter_Click(object sender, ImageClickEventArgs e)
        {
            this.ResetFilter();
            this.GetLogs();
        }

        protected void cmdDeleteLogs_Click(object sender, ImageClickEventArgs e)
        {
            this.DeleteLogs();
            this.GetLogs();
        }

        private void FillHeaders()
        {
            this.cboHeader.Items.Clear();
            ListItem li = new ListItem(">> Select a header <<");
            li.Value = "0";
            this.cboHeader.Items.Add(li);

            foreach (Library.Logging.LogInfo loginfo in _LogManager.GetLogInfos())
            {
                li = new ListItem();
                li.Text = loginfo.LogHeader;
                li.Value = loginfo.LogHeader;
                this.cboHeader.Items.Add(li);
            }
        }

        private void DeleteLogs()
        {
            List<int> ids = new List<int>();
//            List<string> str = new List<string>() // COMMENTED BY CODEIT.RIGHT;

            foreach (GridViewRow row in GridView1.Rows)
            {
                CheckBox checkbox = (CheckBox)row.FindControl("chkRows");

                if (checkbox.Checked)
                {
                    ids.Add(int.Parse(row.Cells[1].Text));
                }
            }
            this._LogManager.DeleteLogsByIDs(ids.ToArray());
        }

        private void ResetFilter()
        {
            this.txtNodeID.Text = "";
            this.txtUserID.Text = "";
            this.cboHeader.SelectedIndex = 0;
            this.txtComment.Text = "";

            this.txtMaxRows.Text = "100";

            this.txtEndDate.Text = "";
            this.txtStartDate.Text = "";
        }

        private void ShowMessage(string message)
        {
            this.pnlGrid.Visible = false;
            this.pnlMessage.Visible = true;
            this.lblMessage.Text = message;
        }

        private void GetLogs()
        {
            System.Nullable<int> maxrow = 100;
            System.Nullable<DateTime> startdate = null;
            System.Nullable<DateTime> enddate = null;
            string header = null;
            System.Nullable<int> userid = null;
            System.Nullable<int> nodeid = null;
            string comment = null;

            if (this.txtMaxRows.Text.Trim() != "")
            {
                int x;
                if (int.TryParse(this.txtMaxRows.Text.Trim(), out x))
                {
                    maxrow = int.Parse(this.txtMaxRows.Text.Trim());
                }
                else
                {
                    this.ShowMessage("Max Rows is not a number!");
                    return;
                }
            }

            if (this.txtNodeID.Text.Trim() != "")
            {
                int x;
                if (int.TryParse(this.txtNodeID.Text.Trim(), out x))
                {
                    nodeid = int.Parse(this.txtNodeID.Text.Trim());
                }
                else
                {
                    this.ShowMessage("Node ID is not a number!");
                    return;
                }
            }

            if (this.txtUserID.Text.Trim() != "")
            {
                int x;
                if (int.TryParse(this.txtUserID.Text.Trim(), out x))
                {
                    userid = int.Parse(this.txtUserID.Text.Trim());
                }
                else
                {
                    this.ShowMessage("User ID is not a number!");
                    return;
                }
            }
            if (this.cboHeader.SelectedIndex != -1 && this.cboHeader.SelectedIndex != 0)
            {
                header = this.cboHeader.SelectedItem.Text;
            }

            if (this.txtStartDate.Text.Trim() != "")
            {
                DateTime x;
                if (DateTime.TryParse(this.txtStartDate.Text.Trim(), out x))
                {
                    startdate = DateTime.Parse(this.txtStartDate.Text.Trim());
                }
                else
                {
                    this.ShowMessage("Startdate is not a date!");
                    return;
                }
            }


            if (this.txtEndDate.Text.Trim() != "")
            {
                DateTime x;
                if (DateTime.TryParse(this.txtEndDate.Text.Trim(), out x))
                {
                    enddate = DateTime.Parse(this.txtEndDate.Text.Trim());
                }
                else
                {
                    this.ShowMessage("Enddate is not a date!");
                    return;
                }
            }

            if (this.txtComment.Text.Trim() != "")
            {
                comment = this.txtComment.Text.Trim();
            }

            global::ClientTools.Library.Logging.LogCollection logs
                = this._LogManager.GetLogs(startdate, enddate, header, userid, nodeid, maxrow, comment);

            this.pnlGrid.Width = Unit.Percentage(100);
            this.GridView1.Width = Unit.Percentage(100);

            this.GridView1.DataSource = logs;
            this.GridView1.DataBind();
            this.GridView1.Width = Unit.Percentage(100);
            this.GridView1.Columns[4].ItemStyle.Width = Unit.Pixel(200);
            this.pnlMessage.Visible = false;
            this.pnlGrid.Visible = true;
            this.lblCount.Text = logs.Count.ToString() + " rows";
            //this.dgLogs.Columns["Datestamp"].DefaultCellStyle.Format = "yyyy-MM-dd HH:mm:ss";
            //this.lblCount.Text = logs.Count.ToString();
        }

        protected void cboHeader_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.GetLogs();
        }

    }
}
